package com.ssyt.store.sys.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssyt.store.common.BaseController;
import com.ssyt.store.common.EasyUIPageModel;
import com.ssyt.store.sys.entity.SysRoleEntity;
import com.ssyt.store.sys.entity.SysUserEntity;
import com.ssyt.store.sys.service.SysLogService;
import com.ssyt.store.sys.service.SysRoleService;

@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

	private static Logger log = LogManager.getLogger(RoleController.class);

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysLogService sysLogService;

	@RequestMapping("/listRole")
	@ResponseBody
	public EasyUIPageModel<SysRoleEntity> listRole(String sort, String order,
			int page, int rows) {
		return sysRoleService.listRole(sort, order, page, rows);
	}

	@RequestMapping("/addRole")
	@ResponseBody
	public String addRole(HttpSession session,SysRoleEntity role, String rolePower) {
		SysUserEntity user = (SysUserEntity) session.getAttribute("user");
		if(user != null){
			sysLogService.addLogs("添加新角色", user.getCode());
		}
		return sysRoleService.addRole(role, rolePower);
	}

	@RequestMapping("/updateRole")
	@ResponseBody
	public String updateRole(HttpSession session,SysRoleEntity role, String rolePower) {
		SysUserEntity user = (SysUserEntity) session.getAttribute("user");
		if(user != null){
			sysLogService.addLogs("更改角色权限", user.getCode());
		}
		return sysRoleService.updateRole(role, rolePower);
	}

	@RequestMapping("/showRoleInfo")
	@ResponseBody
	public SysRoleEntity showRoleInfo(String id) {
		return sysRoleService.showRoleInfo(id);
	}

	@RequestMapping("/updateRoleState")
	@ResponseBody
	public Integer updateRoleState(SysRoleEntity role) {
		return sysRoleService.updateRoleState(role);
	}

	@RequestMapping("/deleteRole")
	@ResponseBody
	public String deleteRole(HttpSession session,String ids) {
		Integer row = sysRoleService.deleteRole(ids);
		SysUserEntity user = (SysUserEntity) session.getAttribute("user");
		if(user != null){
			sysLogService.addLogs("删除角色", user.getCode());
		}
		if (row > 0) {
			return "success";
		}
		return "";
	}

	@RequestMapping("/searchByCondition")
	@ResponseBody
	public EasyUIPageModel<SysRoleEntity> searchByCondition(String sort,
			String order, Integer page, Integer rows, String searchName,
			Integer state) {
		return sysRoleService.listRoleBySearch(sort, order, page, rows,
				searchName, state);
	}

	/**
	 * 用户管理界面的角色选择下拉框
	 * 
	 * @return
	 */
	@RequestMapping("/listUserRoleCombobox/{cate}")
	@ResponseBody
	public List<SysRoleEntity> listUserRoleCombobox(@PathVariable String cate) {
		System.out.println("cate:" + cate);
		return sysRoleService.listUserRoleCombobox(cate);
	}
}
